import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../components/Home.vue'
import Login from '../views/Login.vue'
import Welcome from '../components/main/Welcome.vue'
import User from '../components/main/User.vue'
import Order from '../components/main/Order.vue'
import Salary from '../components/main/Salary.vue'
import Stock from '../components/main/Stock.vue'
import Staff from '../components/main/Staff.vue'
import Salarymg from '../components/main/Salarymg.vue'
import Boss from '../components/main/Boss.vue'
import Myorder from '../components/main/Myorder.vue'
import Apply from '../components/main/Apply.vue'
import Applycheck from '../components/main/Applycheck.vue'
import Checktips from '../components/main/Checktips.vue'
import Checkorder from '../components/main/Checkorder.vue'
import Trader from '../components/main/Trader.vue'
import Clientele from '../components/main/Clientele.vue'
import Products from '../components/main/Products.vue'

Vue.use(VueRouter)

const routes = [
  {path: '/', redirect: '/login'},
  {path: '/login', component: Login},
  {path: '/home', component: Home,redirect:"/welcome",
  children:[
    {path: '/welcome', component: Welcome},
    {path: '/myorder', component: Myorder},
    {path: '/apply', component: Apply},
    {path: '/applycheck', component: Applycheck},
    {path: '/trader', component: Trader},
    {path: '/checkorder', component: Checkorder},
    {path: '/products', component: Products},
    {path: '/checktips', component: Checktips},
    {path: '/clientele', component: Clientele},
    {path: '/user', component: User},
    {path: '/orders', component: Order},
    {path: '/salary', component: Salary},
    {path: '/stock', component: Stock},
    {path: '/staff', component: Staff},
    {path: '/salarymg', component: Salarymg},
    {path: '/boss', component: Boss},
  ]},
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next) => {

  if(to.path === '/login')  return next()

  let token = window.sessionStorage.getItem('token')

  if(token) return next()

  next("/login")
})

export default router
